<?php
// +----------------------------------------------------------------------
// | User: zq
// +----------------------------------------------------------------------
// | Time: 2021/12/6 17:05
// +----------------------------------------------------------------------

namespace App\Http\Controllers\Wx;

use App\CodeResponse;
use App\Exceptions\BusinessException;
use App\Services\User\AddressService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class AddressController extends WxController
{
    /**
     * 获取用户地址列表
     * @return JsonResponse
     */
    public function list()
    {
        $list = AddressService::getInstance()->getAddressListByUserId($this->user()->id);
        return $this->successPaginate($list);
    }

    /**
     * 收货地址详情
     * @param  Request  $request
     * @return JsonResponse
     */
    public function detail(Request $request)
    {
        $id = $request->input('id', 0);
        $address = AddressService::getInstance()->getAddress($this->user()->id, $id);
        if (is_null($address)) {
            $this->fail(CodeResponse::PARAM_VALUE_ILLEGAL);
        }
        return $this->success($address);
    }

    // todo 未完成
    public function save(Request $request)
    {
        $id = $request->input('id', 0);
        $address = AddressService::getInstance()->getAddress($this->user()->id, $id);
        if (is_null($address)) {
            $this->fail(CodeResponse::PARAM_VALUE_ILLEGAL);
        }

    }

    /**
     * 地址删除
     * @param  Request  $request
     * @return JsonResponse
     * @throws BusinessException
     */
    public function delete(Request $request)
    {
        $id = $request->input('id', 0);
        if (empty($id) && !is_numeric($id)) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        AddressService::getInstance()->delete($this->user()->id, $id);
        return $this->success();
    }
}